package at.fhv.components.presentation;

import java.awt.Color;
import java.awt.Graphics;

import javax.media.jai.PlanarImage;
import javax.swing.JPanel;

import at.fhv.framework.CommonImageProcessingHelper;
import at.fhv.framework.events.ImageEvent;
import at.fhv.framework.listeners.ImageProcessingListener;
import at.fhv.framework.listeners.RefireListener;

/**
 * The ImagePanel class.
 * 
 * @author Michael Sieber
 */
public class ImagePanel extends JPanel implements ImageProcessingListener,
		RefireListener {
	private static final long serialVersionUID = -505263769268453439L;
	private CommonImageProcessingHelper _helper;
	private PlanarImage _image;

	/**
	 * Create a new ImagePanel
	 */
	public ImagePanel() {
		_helper = new CommonImageProcessingHelper(this);
	}

	/*
	 * {@inheritDoc}
	 */
	@Override
	public void imageChanged(ImageEvent event) {
		_image = event.getImage();
		repaint();
	}

	/*
	 * {@inheritDoc}
	 */
	@Override
	public void paint(Graphics graphic) {
		graphic.setColor(Color.black);

		if (_image != null) {
			graphic.clearRect(0, 0, getWidth(), getHeight());
			graphic.drawRect(0, 0, _image.getWidth() - 1,
					_image.getHeight() - 1);
			graphic.drawImage(_image.getAsBufferedImage(), 0, 0,
					getBackground(), this);
		} else {
			graphic.drawRect(0, 0, this.getWidth() - 1, this.getHeight() - 1);
		}
	}

	/*
	 * {@inheritDoc}
	 */
	@Override
	public void addRefireListener(RefireListener listener) {
		_helper.addRefireListener(listener);
	}

	/*
	 * {@inheritDoc}
	 */
	@Override
	public void removeRefireListener(RefireListener listener) {
		_helper.removeRefireListener(listener);
	}

	/*
	 * {@inheritDoc}
	 */
	@Override
	public void refire() {
		// nothing to do here
	}

}
